<template>
  <div class="query-form">
    <!-- 查询表单区域 -->
    <el-form :model="queryParams" inline label-width="80px">
      <el-form-item label="姓名">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable />
      </el-form-item>
      <el-form-item label="身份证号">
        <el-input v-model="queryParams.idCard" placeholder="请输入身份证号" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
    
    <!-- 查询结果区域 -->
    <div v-if="queryResult" class="mt-4 p-4 bg-white rounded-lg shadow">
      <h3 class="text-lg font-semibold mb-4">查询结果</h3>
      <el-table :data="queryResult.list" stripe>
        <el-table-column prop="name" label="姓名"></el-table-column>
        <el-table-column prop="idCard" label="身份证号"></el-table-column>
        <el-table-column prop="age" label="年龄"></el-table-column>
        <el-table-column prop="gender" label="性别"></el-table-column>
        <el-table-column prop="address" label="地址"></el-table-column>
      </el-table>
      
      <!-- 分页控件 -->
      <el-pagination
        class="mt-4"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryResult.total"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryResult.pageSize"
        v-model:current-page="queryResult.currentPage"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    
    <!-- 加载状态 -->
    <div v-if="loading" class="mt-4 p-4 bg-white rounded-lg text-center">
      <el-loading v-show="loading" text="正在查询..."></el-loading>
    </div>
    
    <!-- 无数据提示 -->
    <div v-else-if="hasSearched && !queryResult" class="mt-4 p-4 bg-white rounded-lg text-center text-gray-500">
      没有找到匹配的数据
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, watch } from 'vue'
import { ElMessage } from 'element-plus'

// 接收所有患者数据
const props = defineProps({
  allPatients: {
    type: Array,
    required: true
  }
})

// 查询参数
const queryParams = reactive({
  name: '',
  idCard: '',
  pageSize: 10,
  currentPage: 1
})

// 查询结果
const queryResult = ref(null)
// 加载状态
const loading = ref(false)
// 是否已经搜索过
const hasSearched = ref(false)

// emits声明
const emit = defineEmits(['query'])

// 查询处理：模糊匹配姓名和身份证号
const handleQuery = async () => {
  if (!queryParams.name && !queryParams.idCard) {
    ElMessage.warning('请输入姓名或身份证号')
    return
  }
  // 模糊过滤
  const result = props.allPatients.filter(item => {
    const nameMatch = queryParams.name ? item.name.includes(queryParams.name) : true
    const idCardMatch = queryParams.idCard ? item.idCard.includes(queryParams.idCard) : true
    return nameMatch && idCardMatch
  })
  emit('query', result)
}

// 重置表单
const resetForm = () => {
  queryParams.name = ''
  queryParams.idCard = ''
  queryParams.currentPage = 1
  queryResult.value = null
  hasSearched.value = false
  emit('query', props.allPatients)
}

// 分页相关方法
const handleSizeChange = (newSize) => {
  queryParams.pageSize = newSize
  handleQuery()
}

const handleCurrentChange = (newPage) => {
  queryParams.currentPage = newPage
  handleQuery()
}

// 初始加载（可选）
onMounted(() => {
  // 如果需要自动加载数据，可以取消下面的注释
  // handleQuery()
})

// 初始化时抛出全部数据
watch(() => props.allPatients, (val) => {
  emit('query', val)
}, { immediate: true })
</script>

<style scoped>
.query-form {
  padding: 20px;
  background-color: #f5f7fa;
  border-radius: 8px;
}
</style>    